const Mock = require('mockjs');
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/elm');

mongoose.connection.on('open',()=>{
    console.log('success connecting database elm');
}).on('error',()=>{
    console.log('failed connecting database elm');
})

const Schema = mongoose.Schema;

// 1. 用户表
const userSchema = new Schema({
    username:String,                           // 用户名
    password:String,                           // 密码
    nickname:String,                           // 昵称
    profile:String,                            // 简介
    phone:String,                              // 电话
    email:String,                              // 邮箱
    bindTaoBao:Boolean,                        // 绑定淘宝
    bindAliPay:Boolean,                        // 绑定支付宝
    bindWeChat:Boolean,                        // 绑定微信
    avatar:{type:String,default:'avatar.png'}, // 头像
    register_time:String,                      // 注册时间
},{
    collection:'user',
    versionKey:false
})
const User = mongoose.model('user',userSchema);
// let newUser = new User({
//     username:'13200001111',                           // 用户名
//     password:'',                           // 密码
//     nickname:'',                           // 昵称
//     profile:'',                            // 简介
//     phone:'13200001111',                              // 电话
//     email:'',                              // 邮箱
//     bindTaoBao:false,                        // 绑定淘宝
//     bindAliPay:false,                        // 绑定支付宝
//     bindWeChat:false,                        // 绑定微信
//     avatar:'avatar.png', // 头像
//     register_time:new Date().getTime(),                      // 注册时间
// })
// newUser.save(err=>{
//     console.log(err);
// })

/*
{
    "_id" : ObjectId("63965baf4938c8491bb06f02"),
    "user_id" : ObjectId("638e13570104eb4342b3aae9"),
    "username" : "15280159663",
    "password" : "aaa111",
    "phone" : 15280159663.0,
    "email" : "",
    "city" : "",
    "avatar" : "avatar.png",
    "register_time" : "1670798255759"
}
{
    "_id" : ObjectId("63965be7c56381a44fc07795"),
    "user_id" : ObjectId("638e19440104eb4342b3aaec"),
    "username" : "13111111111",
    "password" : "",
    "phone" : 13111111111.0,
    "email" : "",
    "city" : "",
    "avatar" : "avatar.png",
    "register_time" : "1670256964032"
}
{
    "_id" : ObjectId("63965bfc5e02dda33ca141a2"),
    "user_id" : ObjectId("638e2273bae70d4946c73199"),
    "username" : "13200001111",
    "password" : "",
    "phone" : 13200001111.0,
    "email" : "",
    "city" : "",
    "avatar" : "avatar.png",
    "register_time" : "1670259315291"
}
*/

// 2. 用户详细信息
// const newUser = new User({
//     user_id:'638e2273bae70d4946c73199',
//     username:'13200001111',                           // 用户名
//     password:'',                           // 密码
//     phone:'13200001111',                              // 电话
//     email:'',                              // 邮箱
//     city:'',                               // 城市
//     avatar:'avatar.png', // 头像
//     register_time:'1670259315291',                      // 注册时间
// })
// newUser.save(err=>{
//     console.log(err)
// })

// 3. 店铺
const shopsSchema = new Schema({
    shop_id:Number,   // 店铺id
    name:String,      // 店铺名
    latitude:Number,  // 纬度
    longitude:Number, // 经度
    phone:String,     // 电话
    category:String,  // 分类
    starting_price:Number,// 起送价
    delivery_fee:Number,// 配送费
    delivery_duration:Number,// 配送时长
    distance:Number,// 距离
    rating:Number,// 评分
    sales_monthly:Number,// 月售量
    tab_recommend:Array,// tab 推荐理由
    discount:{
        activities:[{
            name:String,
            value:Array
        }],
        welfares:[{
            name:String,
            value:String
        }],
        service:Array,
    },// 优惠（活动、红包、服务）
    poster:String, // 海报
},{
    collection:'shops',
    versionKey:false
})
const Shop = mongoose.model('shops',shopsSchema);

// 店铺详情
const ShopDetailSchema = new Schema({
    shop_id:{
        type:Schema.Types.ObjectId,
        ref:'Shop'
    },
    name:String,
    shopAvatarSrc:String,
    posterSrc:String,
    service:Array,    // 服务
    coupon:{
        unlimited:String,
        hotWelfares:String
    },                  // 优惠券
    discountTag:Array,   // 折扣
    rating:Number,
    rating_percent:Number,
    rating_desc:String,
    rating_taste:Number,
    rating_packing:Number,
    rating_delivery:Number,
    score_tags:{
        all:Number,
        newest:Number,
        good:Number,
        bad:Number,
        media:Number,
    },
    phone:String,     // 电话
    address:String,    //地址
    category:String,  // 分类
    opening_time:String,  //营业时间
    starting_price:Number,// 起送价
    delivery_fee:Number,// 配送费
    shop_album:Array,      //商家相册
},{
    collection:'shopDetail',
    versionKey:false
})
const ShopDetail = mongoose.model('shopDetail',ShopDetailSchema);

// 店铺菜单
const menuSchema = new Schema({
    shop_id:{
        type:Schema.Types.ObjectId,
        ref:'Shop'
    },
    menuList:[
        {
            title:String,
            // detail:[{
            //     foodName:String,
            //     foodImgSrc:String,
            //     foodDesc:String,
            //     foodSaleMonth:Number,
            //     foodRating:Number,
            //     foodPrice:Number,
            //     specifications:[
            //         {
            //             name:String,
            //             value:Array
            //         }
            //     ]
            // }]
            detail:[{
                type:Schema.Types.ObjectId,
                ref:'food'
            }]
        }
    ],
},{
    collection:'menu',
    versionKey:false
})
const Menu = mongoose.model('menu',menuSchema);

// 评论
const commentSchema = new Schema({
    shop_id:{
        type:Schema.Types.ObjectId,
        ref:'Shop'
    },
    user_id:{
        type:Schema.Types.ObjectId,
        ref:'User'
    },
    isVIP:Boolean,
    isOldCustomer:Boolean,
    commentRatingPercent:Number,
    commentTags:[
        {
            title:String,
            result:String
        }
    ],
    commentHighlight:Array,
    commentText:String,
    commentImgSrc:Array,
    recommendFood:String,
    thumbsUp:Number
},{
    collection:'comment',
    versionKey:false
})
const Comment = mongoose.model('comment',commentSchema);

// 商品
const foodSchema = new Schema({
    shop_id:{
        type:Schema.Types.ObjectId,
        ref:'shop'
    },
    title_index:Number,
    foodName:String,
    foodImgSrc:String,
    foodDesc:String,
    foodSaleMonth:Number,
    foodRating:Number,
    foodPrice:Number,
    specifications:[
        {
            name:String,
            value:Array
        }
    ],
    pack_fee:Number
},{
    collection:'food',
    versionKey:false
})
const Food = mongoose.model('food',foodSchema);

// let newFood = new Food({
//     shop_id: '6390a8b7b3664d87dfffb483',
//     title_index:2,
//     foodName : "红烧肉豆腐盖饭",
//     foodImgSrc : "food.jpeg",
//     foodDesc : "肉质肥瘦相间，入口爽滑。",
//     foodSaleMonth : 49,
//     foodRating : 100,
//     foodPrice : 51.88,
//     specifications : [],
// })
// newFood.save(err=>{
//     console.log('newFood',err);
// })

// 购物车
const cartSchema = new Schema({
    user_id:{
        type:Schema.Types.ObjectId,
        ref:'user'
    },
    shop_id:{
        type:Schema.Types.ObjectId,
        ref:'shops'
    },
    food_id:{
        type:Schema.Types.ObjectId,
        ref:'food'
    },
    foodSpec:Array, // 商品可选值
    foodNum:Number,  // 数量
},{
    collection:'cart',
    versionKey:false
})
const Cart = mongoose.model('cart',cartSchema);

const orderSchema = new Schema({
    user_id:{
        type:Schema.Types.ObjectId,
        ref:'user'
    },
    shop_id:{
        type:Schema.Types.ObjectId,
        ref:'shops'
    },
    food:[
        {
            food_id:{
                type:Schema.Types.ObjectId,
                ref:'food'
            },
            food_spec:String,
            food_num:Number,
            food_actually_pay:Number,

        }
    ],
    pack_fee:Number,
    actually_delivery_fee:Number,
    address:String,
    call_name:String,
    phone:String,
    remark:String,
    deliver_time:String,
    payment:String,
    create_time:String,
    status:String,
    isEvaluate:Boolean
},{
    collection:'order',
    versionKey:false
})
const Order = mongoose.model('order',orderSchema);

// let newOrder = new Order({
//     user_id:'63965baf4938c8491bb06f02',
//     shop_id:'6390a8b7b3664d87dfffb47d',
//     food:[
//         {
//             food_id:'63984a5d21bb9fb4c895008e',
//             food_spec:'微辣',
//             food_num:1,
//             food_actually_pay:17.89,
//         },
//         {
//             food_id:'63984aa2e88f28919befb169',
//             food_spec:'不辣',
//             food_num:1,
//             food_actually_pay:17.89,
//         },
//         {
//             food_id:'63984abdd3f7e9904efd10d3',
//             food_spec:'不辣',
//             food_num:1,
//             food_actually_pay:15.89,
//         },
//     ],
//     pack_fee:10,
//     actually_delivery_fee:0,
//     address:Mock.mock('@county(true)'),
//     call_name:Mock.mock('@cname'),
//     phone:Mock.mock(/^1[356789][0-9]{9}$/),
//     remark:Mock.mock('@csentence(3, 5)'),
//     deliver_time:'尽快送达',
//     payment:'商家配送',
//     create_time:Mock.mock('@datetime("T")')
// })
// newOrder.save(err=>{console.log(err)});


// const newShopDetail = new ShopDetail({
//     shop_id:'6390a8b7b3664d87dfffb47d',
//     name:'称很代价集',
//     shopAvatarSrc:'110000799.webp',
//     posterSrc:'poster.webp',
//     service:['约55分钟','月售2000+','支持自取','食无忧'],    // 服务
//     coupon:{
//         unlimited:'6',
//         hotWelfares:'14'
//     },                  // 优惠券
//     discountTag:['55减30','免配送费','下单返奖励'],   // 折扣
//     rating:4.8,
//     rating_percent:90,
//     rating_desc:'高于附近48.9%的商家',
//     rating_taste:4.8,
//     rating_packing:4.8,
//     rating_delivery:96,
//     score_tags:{
//         all:515,
//         newest:0,
//         good:438,
//         bad:33,
//         media:111,
//     },
//     phone:'12200000000',     // 电话
//     address:'福建省福州市闽侯县上街镇溪源宫路200号闽江学院第四食堂二楼',    //地址
//     opening_time:'周一至周日 09:30-13:30，16:00-19:30',  //营业时间
//     starting_price:10,// 起送价
//     delivery_fee:0,// 配送费
//     shop_album:['shopImage-1.jpeg','shopImage-2.jpeg'],      //商家相册
// })
// newShopDetail.save(err=>{
//     console.log('err',err)
// })

// const newMenu = new Menu({
//     shop_id:'6390a8b7b3664d87dfffb47d',
//     menuList:[
//         {
//             title:'热销',
//             detail:[
//                 '63984991815e1333bf890992',
//                 '639848e548f345650bc15446',
//                 '639849f3a61c86e79f0ea232',
//                 '63984a3096e4996921956457',
//                 '63984a5d21bb9fb4c895008e'
//             ]
//         },
//         {
//             title:'黄焖鸡米饭',
//             detail:[
//                 '63984aa2e88f28919befb169',
//                 '63984abdd3f7e9904efd10d3',
//                 '63984adb478be6c19849ecfe',
//                 '63984af47fa3c5fb7fee949c',
//                 '63984b065719605701a77698'
//             ]
//         },
//         {
//             title:'红烧肉系列盖浇饭',
//             detail:[
//                 '63984b251919bd578be079a7',
//                 '63984b49cf9109f82f3b17e3',
//                 '63984b69e1448e02b74c538b',
//                 '63984b775531507ed76d9ceb'
//             ]
//         },
//     ],
// })
// newMenu.save(err=>{console.log('err',err)})

// const newComment = new Comment({
//     shop_id:'6390a8b7b3664d87dfffb47d',
//     user_id:'638e19440104eb4342b3aaec',
//     isVIP:true,
//     isOldCustomer:false,
//     commentRatingPercent:100,
//     commentTags:[
//         {
//             title:'味道',
//             result:'5星'
//         },
//         {
//             title:'包装',
//             result:'5星'
//         },
//         {
//             title:'配送',
//             result:'满意'
//         },
//     ],
//     commentHighlight:['沙拉烤肉拌饭','荷包蛋'],
//     commentText:'好吃！肉挺多，配菜也好吃👍🏻👍🏻',
//     commentImgSrc:['comment-food-01.jpg'],
//     recommendFood:'孜然烤肉拌饭',
//     thumbsUp:1
// })
// newComment.save(err=>{console.log('err',err)})

module.exports = {
    User,
    Shop,
    ShopDetail,
    Menu,
    Comment,
    Cart,
    Food,
    Order
}